<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" size="xl" title="新建知识库" scrollable no-close-on-backdrop centered>
        <div class="d-block text-center">
            <!--begin::div-->
            <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed">
                <div class="m-portlet__body">
                    <div class="form-group m-form__group row">
                        <label class="col-lg-1 col-form-label">标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题</label>
                        <div class="col-lg-4">
                            <input class="form-control" type="text" maxlength="100" v-model="knowledgeForm.title" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-1 col-form-label">类&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;型</label>
                        <div class="col-lg-2">
                            <el-select v-model="knowledgeForm.type" placeholder="请选择">
                                <el-option value="">请选择</el-option>
                                <el-option
                                    v-for="item in [{value:'0',label:'平台问题'},{value:'1',label:'学习知识'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-1 col-form-label">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态</label>
                        <div class="col-lg-2">
                            <el-select v-model="knowledgeForm.state" placeholder="请选择">
                                <el-option value="">请选择</el-option>
                                <el-option
                                    v-for="item in [{value:0,label:'待解决'},{value:1,label:'已解决'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-1 col-form-label">级&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别</label>
                        <div class="col-lg-2">
                            <el-select v-model="knowledgeForm.level" placeholder="请选择">
                                <el-option value="">请选择</el-option>
                                <el-option
                                    v-for="item in [{value:1,label:'紧急'},{value:2,label:'正常'},{value:3,label:'一般'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-1 col-form-label" style="padding-top: calc(0.65rem + 20px);">内&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;容</label>
                        <div class="col-lg-11">
                            <quill-editor 
                                class="ql-editor"
                                v-model="knowledgeForm.content" 
                                ref="myQuillEditor" 
                                :options="editorOption" 
                                @blur="onEditorBlur($event)" 
                                @focus="onEditorFocus($event)"
                                @change="onEditorChange($event)">
                            </quill-editor>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button> 
            <button type="button" class="btn btn-light-primary font-weight-bold mr-2" block @click="restForm">
                <i class="icon-2x flaticon2-reply"></i> 重置
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="addXtKnowledge">
                <i class="icon-xl fas fa-save"></i> 保存
            </button>
        </template>
    </b-modal>        
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js"; 
import { handleNotify,handleAlert,handleConfirm,showWating,closeWating} from "@/core/util/jehcUtil.js";
import "@/assets/css/quill.css";
export default {
    data(){
        return {            
            // 富文本编辑器配置
            editorOption: {
                placeholder: '请输入',
                theme: 'snow',//bubble,snow
                height:500,
                modules:{     
                    imageDrop: true,      //图片拖拽
                    imageResize: {          //放大缩小
                    displayStyles: {
                        backgroundColor: "black",
                        border: "none",
                        color: "white"
                    },
                    modules: ["Resize", "DisplaySize", "Toolbar"]
                    },          
                    toolbar: [
                        ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
                        ['blockquote', 'code-block'], // 引用  代码块-----['blockquote', 'code-block']
                        [{ header: 1 }, { header: 2 }], // 1、2 级标题-----[{ header: 1 }, { header: 2 }]
                        [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
                        [{ script: 'sub' }, { script: 'super' }], // 上标/下标-----[{ script: 'sub' }, { script: 'super' }]
                        [{ indent: '-1' }, { indent: '+1' }], // 缩进-----[{ indent: '-1' }, { indent: '+1' }]
                        [{'direction': 'rtl'}], // 文本方向-----[{'direction': 'rtl'}]
                        [{ size: ['small', false, 'large', 'huge'] }], // 字体大小-----[{ size: ['small', false, 'large', 'huge'] }]
                        [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
                        [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色-----[{ color: [] }, { background: [] }]
                        [{ font: [] }], // 字体种类-----[{ font: [] }]
                        [{ align: [] }], // 对齐方式-----[{ align: [] }]
                        ['clean'], // 清除文本格式-----['clean']
                        ['image', 'video'] ,// 链接、图片、视频-----['link', 'image', 'video']
                    ]
                }
            },
            knowledgeForm:{
                title: "",
                type: "",
                state:"",
                level:"",
                content:"",
            }
        }
    },
    components: {
        
    },
    watch: {
    },
    mounted() {
        
        
    },
    methods: {
        addXtKnowledge(){
            if(this.knowledgeForm.title === ""){
                handleAlert("请输入标题", "warning", 3)
                return;
            }
            if(this.knowledgeForm.type === ""){
                handleAlert("请选择类型", "warning", 3)
                return;
            }
            if(this.knowledgeForm.state === ""){
                handleAlert("请选择状态", "warning", 3)
                return;
            }
            if(this.knowledgeForm.level === ""){
                handleAlert("请选择级别", "warning", 3)
                return;
            }
            // 保存前提示
            this.$confirm("确定保存?", "提示", {type: "warning",}).then(() => {
                apiUtil.post(process.env.VUE_APP_SYS_API+"/xtKnowledge/add", this.knowledgeForm).then(({ data }) => {
                    if(data.success){
                        handleAlert("创建知识库成功", "success", 3);
                        this.hideModal();//关闭窗体
                        this.$emit("change",data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
                    }else {
                        handleAlert("创建知识库失败", "error", 3);
                    }            
                });
            }).catch(()=>{});//注意这里，这里是重点！！！;   
        },
        showModal() {
            this.restForm();
            this.$refs['my-modal'].show();
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        //失去焦点事件
        onEditorBlur(quill) {
            console.log('editor blur!', quill)
        },
		//获得焦点事件
        onEditorFocus(quill) {
            console.log('editor focus!', quill)
        },
		//准备富文本编辑器
        onEditorReady(quill) {
            console.log('editor ready!', quill)
        },
		//内容改变事件
        onEditorChange({ quill, html, text }) {
            console.log('editor change!', quill, html, text)
            this.knowledgeForm.content = html
        },
        restForm(){
            //重置
            Object.assign(this.$data.knowledgeForm, this.$options.data().knowledgeForm);
        },
   }
}
</script>